<template>
  <el-dialog
    v-model="dialogShow"
    :close-on-click-modal="false"
    :destroy-on-close="false"
    custom-class="mydialog"
    width="600px"
    :title="titleContent"
    @close="close"
  >
    <el-form
      ref="formRef"
      class="myForm"
      :model="formData"
      :label-width="formLabelWidth"
      size="small"
      :rules="formRules"
    >
      <el-form-item
        class="form-item"
        label="选择地区"
        prop="area"
      >
        <el-cascader
          v-model="formData.area"
          :popper-append-to-body="false"
          style="width: 100%;"
          :options="citys"
          placeholder="请选择适用地区"
          :props="{ checkStrictly: true, expandTrigger: 'hover', label: 'regionName', value:'regionCode' }"
          clearable
        />
      </el-form-item>
      <el-form-item
        class="form-item"
        label="市场开发阶段"
        prop="sckfjd"
      >
        <el-select
          v-model="formData.sckfjd"
          style="width: 100%"
          clearable
          filterable
          placeholder="请选择价格属性"
        >
          <el-option
            label="市场推广"
            value="市场推广"
          />
          <el-option
            label="商业铺货"
            value="商业铺货"
          />
        </el-select>
      </el-form-item>
      <el-form-item
        class="form-item"
        label="营销模式"
        prop="xsms"
      >
        <el-checkbox-group v-model="formData.xsms">
          <el-checkbox :label="1">终端直销</el-checkbox>
          <el-checkbox :label="2">代理</el-checkbox>
        </el-checkbox-group>
      </el-form-item>
      <el-form-item
        class="form-item"
        label="控销设置"
      >
        <div
          class="type-container"
          style="margin-bottom: 30px;"
        >
          <div style="width: 140px;">按终端类型</div>
          <div class="type-select">
            <el-radio-group v-model="formData.is_type_sell">
              <el-radio :label="1">允许营销</el-radio>
              <el-radio :label="0">禁止营销</el-radio>
            </el-radio-group>
          </div>
          <div class="end-user-select">
            <el-cascader
              v-model="formData.end_user1"
              :popper-append-to-body="false"
              style="width: 100%;"
              :options="endUserList"
              placeholder="请选择终端"
              :props="{ multiple: true }"
              clearable
              collapse-tags
            />
          </div>
        </div>
        <div class="name-container">
          <div style="width: 140px;">按终端名称精控</div>
          <div class="type-select">
            <el-radio-group v-model="formData.is_name_sell">
              <el-radio :label="1">允许营销</el-radio>
              <el-radio :label="0">禁止营销</el-radio>
            </el-radio-group>
          </div>
          <div
            class="name-select"
            style="display: flex;justify-content: space-between;"
          >
            <el-select
              v-model="formData.end_user2"
              style="width: 330px"
              filterable
              allow-create
              default-first-option
              placeholder="请选择或输入终端名称"
            >
              <el-option
                v-for="item in options"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              />
            </el-select>
            <el-button
              type="text"
              style="width: 100px;padding-left: 20px;padding-right: 10px;box-sizing: border-box;font-size: 14px;"
              @click="addItem"
            >
              添加至名单</el-button>
          </div>
          <div
            v-if="nameTableList.length"
            class="name-list"
            style="margin-top: 20px;"
          >
            <el-table
              :show-header="false"
              :data="nameTableList"
              border
              max-height="300"
            >
              <el-table-column
                prop="name"
                width="330px"
              >
                <template #default="scope">
                  <span>{{ scope.row.name }}</span>
                </template>
              </el-table-column>
              <el-table-column align="right">
                <template #default="scope">
                  <el-button
                    type="text"
                    @click="removeItem(scope.row,scope.$index)"
                  >从名单移出</el-button>
                </template>
              </el-table-column>
            </el-table>
          </div>
        </div>
      </el-form-item>
    </el-form>
    <template #footer>
      <div
        class="dialog-footer"
        style="display:flex;justify-content:center;align-items:center;"
      >
        <el-button
          class="border-btn"
          size="small"
          style="margin-right: 20px;"
          @click="close"
        >取消</el-button>
        <el-button
          class="main-btn"
          size="small"
          type="primary"
          @click="save"
        >保存</el-button>
      </div>
    </template>
  </el-dialog>
</template>

<script>
import {defineComponent, reactive, ref, watch} from 'vue'
import area from '@/utils/area.js'

export default defineComponent({
  props: {
    types: {
      type: String,
      default: 'create',
    },
  },
  setup(props, context) {
    const citys = area
    var formRef = ref(null)
    var formLabelWidth = ref(120)
    var dialogShow = ref(false)
    var dialogType = ref(props.types)
    var titleContent = ref('添加市场规则')
    var nameTableList = reactive([
      {
        name: '陕西省人民医院',
      },
    ])
    var endUserList = reactive([
      {
        value: 'guide',
        label: 'Guide',
        disabled: true,
        children: [
          {
            value: 'disciplines',
            label: 'Disciplines',
            children: [
              {
                value: 'consistency',
                label: 'Consistency',
              },
              {
                value: 'feedback',
                label: 'Feedback',
              },
              {
                value: 'efficiency',
                label: 'Efficiency',
              },
              {
                value: 'controllability',
                label: 'Controllability',
              },
            ],
          },
          {
            value: 'navigation',
            label: 'Navigation',
            children: [
              {
                value: 'side nav',
                label: 'Side Navigation',
              },
              {
                value: 'top nav',
                label: 'Top Navigation',
              },
            ],
          },
        ],
      },
      {
        value: 'component',
        label: 'Component',
        children: [
          {
            value: 'basic',
            label: 'Basic',
            children: [
              {
                value: 'layout',
                label: 'Layout',
              },
              {
                value: 'color',
                label: 'Color',
              },
              {
                value: 'typography',
                label: 'Typography',
              },
              {
                value: 'icon',
                label: 'Icon',
              },
              {
                value: 'button',
                label: 'Button',
              },
            ],
          },
          {
            value: 'form',
            label: 'Form',
            children: [
              {
                value: 'radio',
                label: 'Radio',
              },
              {
                value: 'checkbox',
                label: 'Checkbox',
              },
              {
                value: 'input',
                label: 'Input',
              },
              {
                value: 'input-number',
                label: 'InputNumber',
              },
              {
                value: 'select',
                label: 'Select',
              },
              {
                value: 'cascader',
                label: 'Cascader',
              },
              {
                value: 'switch',
                label: 'Switch',
              },
              {
                value: 'slider',
                label: 'Slider',
              },
              {
                value: 'time-picker',
                label: 'TimePicker',
              },
              {
                value: 'date-picker',
                label: 'DatePicker',
              },
              {
                value: 'datetime-picker',
                label: 'DateTimePicker',
              },
              {
                value: 'upload',
                label: 'Upload',
              },
              {
                value: 'rate',
                label: 'Rate',
              },
              {
                value: 'form',
                label: 'Form',
              },
            ],
          },
          {
            value: 'data',
            label: 'Data',
            children: [
              {
                value: 'table',
                label: 'Table',
              },
              {
                value: 'tag',
                label: 'Tag',
              },
              {
                value: 'progress',
                label: 'Progress',
              },
              {
                value: 'tree',
                label: 'Tree',
              },
              {
                value: 'pagination',
                label: 'Pagination',
              },
              {
                value: 'badge',
                label: 'Badge',
              },
            ],
          },
          {
            value: 'notice',
            label: 'Notice',
            children: [
              {
                value: 'alert',
                label: 'Alert',
              },
              {
                value: 'loading',
                label: 'Loading',
              },
              {
                value: 'message',
                label: 'Message',
              },
              {
                value: 'message-box',
                label: 'MessageBox',
              },
              {
                value: 'notification',
                label: 'Notification',
              },
            ],
          },
          {
            value: 'navigation',
            label: 'Navigation',
            children: [
              {
                value: 'menu',
                label: 'Menu',
              },
              {
                value: 'tabs',
                label: 'Tabs',
              },
              {
                value: 'breadcrumb',
                label: 'Breadcrumb',
              },
              {
                value: 'dropdown',
                label: 'Dropdown',
              },
              {
                value: 'steps',
                label: 'Steps',
              },
            ],
          },
          {
            value: 'others',
            label: 'Others',
            children: [
              {
                value: 'dialog',
                label: 'Dialog',
              },
              {
                value: 'tooltip',
                label: 'Tooltip',
              },
              {
                value: 'popover',
                label: 'Popover',
              },
              {
                value: 'card',
                label: 'Card',
              },
              {
                value: 'carousel',
                label: 'Carousel',
              },
              {
                value: 'collapse',
                label: 'Collapse',
              },
            ],
          },
        ],
      },
      {
        value: 'resource',
        label: 'Resource',
        children: [
          {
            value: 'axure',
            label: 'Axure Components',
          },
          {
            value: 'sketch',
            label: 'Sketch Templates',
          },
          {
            value: 'docs',
            label: 'Design Documentation',
          },
        ],
      },
    ])
    const open = () => {
      dialogShow.value = true
    }
    const close = () => {
      dialogShow.value = false
    }
    var formData = reactive({
      area: '',
      sckfjd: '',
      xsms: [1],
      is_type_sell: 1,
      end_user1: [],
      is_name_sell: 1,
      end_user2: [],
    })
    var formRules = reactive({
      area: [
        {
          required: true,
          message: '请选择地区',
          trigger: 'change',
        },
      ],
      sckfjd: [
        {
          required: true,
          message: '请选择市场开发阶段',
          trigger: 'change',
        },
      ],
    })
    const save = () => {
      formRef.value.validate((valid) => {
        if (valid) {
          close()
        } else {
          console.log('error submit!!')
          return false
        }
      })
    }
    const removeItem = (row, index) => {
      nameTableList.splice(index, 1)
    }
    const addItem = () => {
      nameTableList.unshift({
        name: formData.end_user2,
      })
      formData.end_user2 = ''
    }
    watch(
      () => props.types,
      (newval, oldval) => {
        dialogType.value = newval
        if (newval === 'create') {
          titleContent.value = '添加市场规则'
        } else if (newval === 'edit') {
          titleContent.value = '编辑市场规则'
        }
      }
    )

    return {
      citys,
      formRef,
      formLabelWidth,
      dialogShow,
      titleContent,
      formData,
      endUserList,
      nameTableList,
      formRules,
      save,
      close,
      open,
      addItem,
      removeItem,
    }
  },
})
</script>
